package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.AdminRoleMapper;
import org.tea.inventory.db.dao.RoleMapper;
import org.tea.inventory.db.dao.RoleMenuMapper;
import org.springframework.stereotype.Service;
import org.tea.inventory.db.domain.AdminRole;
import org.tea.inventory.db.domain.Role;
import org.tea.inventory.db.domain.RoleMenu;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private AdminRoleMapper adminRoleMapper;


    /**
     * 添加角色
     *
     * @param record 角色
     * @return int 插入条数
     */
    public int insertSelective(Role record){
        return roleMapper.insertSelective(record);
    }

    /**
     * 根据id查询角色
     *
     * @param id 角色id
     * @return Role 角色
     */
    public Role selectByPrimaryKey(Integer id){
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     *
     * 根据id更新角色信息
     *
     * @param record 角色
     * @return int 跟新条数
     */
    public int updateByPrimaryKeySelective(Role record){
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    /**
     *
     * 分页查询角色表,如果roleName为空，则查询全部
     *
     * @param roleName 模糊查询
     * @param page
     * @param size
     * @return list<Role>
     */
    public Map<String,Object> selectByRoleName(String roleName, Integer page, Integer size){
        Role example = new Role();
        example.setRoleName(roleName);

        PageHelper.startPage(page, size);
        List<Role> list = roleMapper.selectByExample(example);

        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());

        return resMap;
    }


    /**
     * 添加角色和菜单关联
     *
     * @param roleId
     * @param menuList
     */
    public void insertRoleMenu(int roleId,List<String> menuList){
        RoleMenu example = new RoleMenu();
        example.setRoleId(roleId);

        roleMenuMapper.deleteByExample(example);

        for (String menu:menuList){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menu);
            roleMenuMapper.insertSelective(roleMenu);
        }

    }

    /**
     * 根据角色查询菜单
     *
     * @param roleId
     * @return List<String>
     */
    public List<String> selectMenuByRoleId(Integer roleId){

        RoleMenu example = new RoleMenu();
        example.setRoleId(roleId);

        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        List<String> resultList = new ArrayList<String>();
        for (RoleMenu rm : list){
            resultList.add(rm.getMenuId());
        }

        return resultList;

    }


    /**
     *
     * 添加admin用户和角色关系
     * @param adminId
     * @param roleList
     */
    public void insertAdminRole(Integer adminId, List<String> roleList) {
        AdminRole example = new AdminRole();
        example.setAdminId(adminId);
        adminRoleMapper.deleteByExample(example);

        for (String roleId:roleList){
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(Integer.parseInt(roleId));
            adminRoleMapper.insertSelective(adminRole);
        }
    }

    /**
     * 查询用户所拥有的菜单
     *
     * @param id
     * @return
     */
    public List<String> selectAdminUserMenuByAdminId(Integer id){
        List<String> list = roleMapper.selectAdminUserMenuByAdminId(id);
        return list;
    }

    /**
     * 查询用户拥有的角色
     *
     * @param adminId 用户id
     * @return
     */
    public List<String> selectAdminUserRoleByAdminId(Integer adminId) {
        AdminRole example = new AdminRole();
        example.setAdminId(adminId);

        List<AdminRole> list = adminRoleMapper.selectByExample(example);
        List<String> resList = new ArrayList<String>();
        for (AdminRole ar:list){
            resList.add(ar.getRoleId()+"");
        }

        return resList;
    }
}
